package hr.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hr.entity.DO.PositionEmployeesDO;
import hr.entity.DO.PositionsDepartmentDO;
import hr.entity.Employee;
import hr.entity.Position;
import hr.entity.vo.PositionEmployeesVo;
import hr.entity.vo.PositionVo;
import hr.service.EmployeesService;
import hr.service.PositionsService;
import hr.utils.Result.R;
import hr.utils.Result.Result;
import hr.utils.Result.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-12-06
 */
@Tag(name = "岗位管理")
@RestController
@Slf4j
@RequestMapping("/positions")
public class PositionsController {
    @Autowired
    private PositionsService positionsService;

    @Autowired
    private EmployeesService employeesService;

    @Operation(summary = "获取岗位列表")
    @PostMapping("/getAllPositions")
    public Result<Object> getAllPositions(@RequestBody PositionVo positionVo) {
        // 使用 positionVo.getPositionName()、positionVo.getPositionStatus() 等访问参数值
        HashMap<String, Object> map = new HashMap<>();
        List<PositionsDepartmentDO> list = positionsService.selectAllPosition(positionVo);
        map.put("positionsList", list);
        IPage<Position> pageInfo = new Page<>(positionVo.getPage(), positionVo.getLimit());
        Integer count = positionsService.selectCount(positionVo);
        map.put("count", count);
        map.put("pageInfo", pageInfo);
        return ResultUtil.success(map);
    }

    @Operation(summary = "编辑岗位")
    @PostMapping("/updatePosition1")
    public Result<Object> updatePosition(@RequestBody Position position) {
        return positionsService.updateById(position) ? ResultUtil.success("岗位信息已成功更新") : ResultUtil.fail("未能更新岗位信息");
    }

    @Operation(summary = "删除岗位")
    @PostMapping("/deletePositions/{positionId}")
    public Result<Object> deletePositions(@PathVariable Integer positionId) throws Exception {
        return positionsService.deletePostByPositionId(positionId) ? ResultUtil.success("删除岗位成功") : ResultUtil.fail("删除岗位失败,请重试");
    }

    @Operation(summary = "新增岗位")
    @PostMapping("/addPositions")
    public Result<Object> addPositions(@RequestBody Position position) {
        boolean add = positionsService.addPositions(position);
        return add ? ResultUtil.success("岗位新增成功") : ResultUtil.fail("岗位新增失败，请重试!");
    }

    @Operation(summary = "岗位下员工查询")
    @PostMapping("/getEmployeeByPositionId")
    public Result<Object> getEmployeeByPositionId(@RequestBody PositionEmployeesVo positionEmployeesVO) {
        HashMap<String, Object> map = new HashMap<>();
        List<PositionEmployeesDO> employeesList = positionsService.getEmployeeByPositionId(positionEmployeesVO);
        map.put("employeesList", employeesList);
        IPage<Employee> pageInfo = new Page<>(positionEmployeesVO.getPage(), positionEmployeesVO.getLimit());
        Integer count = employeesService.getCount(positionEmployeesVO);
        map.put("count", count);
        map.put("pageInfo", pageInfo);
        return ResultUtil.success(map);
    }

    @Operation(summary = "查询岗位列表(不分页)")
    @PostMapping("/getPositions")
    public Result<Object> getPositions() {
        List<Position> positionList = positionsService.getPositionIdAndName();
        return ResultUtil.success(positionList);
    }

    @Operation(summary = "查询各个岗位的薪资、人员数目（报表）")
    @PostMapping("/getPositionsSalaryAndPeople")
    public R getPositionsSalary() {
        Map<String,Object> map = positionsService.handlePositionsSalaryAndPeople();
        return R.success(map);
    }

}
